Ext.onReady(function() {
    Ext.SSL_SECURE_URL = "../../ext-3.2.1/resources/images/default/s.gif";
    Ext.BLANK_IMAGE_URL = "../../ext-3.2.1/resources/images/default/s.gif";
//    Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
    Ext.util.CSS.swapStyleSheet('theme', currentTheme);
    Ext.QuickTips.init();
    Ext.form.Field.prototype.msgTarget = 'side';

    var pageSize = 20;
    new Ext.Viewport({
        region:'center',
        layout:'fit',
        items:grid
    });

    var addButton = new Ext.Toolbar.Button({
        icon    : "../../ext-3.2.1/examples/shared/icons/fam/add.png",
        id      : 'addButton',
        cls: 'x-btn-text-icon',
        text    : '添加',
        tooltip : '添加一条新的记录',
        handler : onAddClick2
    });
    var modButton = new Ext.Toolbar.Button({
        icon    : "../../ext-3.2.1/examples/shared/icons/fam/edit.png",
        id      : 'modButton',
        cls: 'x-btn-text-icon',
        text    : '修改',
        tooltip : '修改一条选中记录。',
        handler : onModClick
    });
    var delButton = new Ext.Toolbar.Button({
        icon    : "../../ext-3.2.1/examples/shared/icons/fam/del.png",
        id      : 'delButton',
        cls: 'x-btn-text-icon',
        text    : '删除',
        tooltip : '删除已选中的记录。',
        handler : onDelClick
    });
    var formPanel ;
    var win;
    function onAddClick(item) {

        var code,name,describe,ddId,dddId;
        var record,id,titleStr;
        titleStr='增加记录'

        if(item=='mod') {
            titleStr='修改记录'
            var selectedCount = sm.getCount();
            if (selectedCount == 0) {
                Ext.Msg.alert('修改记录', '未选中任何一条记录，请选择要修改的记录！');
                return;
            } else if (selectedCount > 1) {
                Ext.Msg.alert('修改记录', '只能选择一条记录，不能同时选择多条记录！');
                return;
            }
            record = sm.getSelected();
            id = record.id;
        }
        dddId = new Ext.form.Hidden({
            name:'dddId',
            id:'dddId'
            //value:id
        });
        code = new Ext.form.TextField({
            fieldLabel: '编码',
            id:'code',
            name: 'code',
            width:100,
            maxLength:8,
            maxLengthText:"最大长度8",
            allowBlank:false
        });
        name = new Ext.form.TextField({
            fieldLabel: '名称',
            id:'name',
            name: 'name',
            width:200,
            maxLength:16,
            maxLengthText:"最大长度16",
            allowBlank:false
        });
        describe = new Ext.form.TextField({
            fieldLabel: '说明',
            id:'describe',
            name: 'describe',
            width:300,
            maxLength:50,
            maxLengthText:"最大长度50"
        });
        ddId = new Ext.form.Hidden({
            name:'ddId',
            id:'ddId',
            value:par_ddId
        });
         var zmark= new Ext.form.TextField({
                fieldLabel: '标记',
                id:'zmark',
                name: 'zmark',
                width:100,
                maxLength:1,
                maxLengthText:"最大长度1"
               // value:gridStore.getById(id).data['zmark']
            });
        if (formPanel != null) {
            formPanel.destroy(true);
            formPanel = null;
        }
        if (!formPanel) {
            formPanel = new Ext.FormPanel({
                labelWidth: 125,
                labelAlign:"right",
                url: '../../dataDictionary/createData.action?optFlag='+item,
                frame:false,
                bodyStyle:'padding:60px 5px 0',
                items: [dddId,code,name,describe,ddId ,zmark]
            });
        }
        if (win != null) {
            win.destroy(true);
            win = null;
        }
        if (!win) {
            win = new Ext.Window({
                id : 'formWin',
                title: titleStr,
                width: 530,
                height:320,
                minWidth: 200,
                minHeight: 200,
                xtype:'window',
                maximizable : false,
                closeAction : 'hide',
                layout: 'fit',
                plain:false,
                closable:true,
                modal:false,
                border:false,
                bodyStyle:'padding:5px;',
                buttonAlign:'center',
                items: formPanel,
                buttons: [{
                    handler: function() {
                        if (formPanel.getForm().isValid()) {
                            formPanel.getForm().submit({
                                waitMsg: '正在处理，请稍等...',
                                reset: true,
                                failure: function(form, action) {
                                    Ext.MessageBox.hide();
                                    Ext.Msg.show({
                                        title: '错误',
                                        msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + action.result.msg + '</span>',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.WARNING
                                    });
                                },
                                success: function(form, action) {
                                    Ext.MessageBox.hide();
                                    Ext.Msg.show({
                                        title: '成功',
                                        msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + action.result.msg + '</span>',
                                        buttons: Ext.MessageBox.OK,
                                        icon: Ext.MessageBox.INFO
                                    });
                                    win.hide();
                                    gridStore.reload();
                                },
                                method:'POST'
                            });
                        }
                    },
                    text: '保存'
                },{
                    handler: function() {
                        win.hide();
                    },
                    text: '取消'
                }]
            });
        }
        if(item=='mod'){
            formPanel.getForm().loadRecord(record)
        }
        win.show();
    }
    var modformPanel ;
    var modwin;
    function onModClick(item) {

        var selectedCount = sm.getCount();
        if (selectedCount == 0) {
            Ext.Msg.alert('修改记录', '未选中任何一条记录，请选择要修改的记录！');
            return;
        } else if (selectedCount > 1) {
            Ext.Msg.alert('修改记录', '只能选择一条记录，不能同时选择多条记录！');
            return;
        }
            var record = sm.getSelected();

            if (modformPanel != null) {
                modformPanel.destroy(true);
                modformPanel = null;
            }
            if (!modformPanel) {
                modformPanel = new Ext.FormPanel({
                    labelWidth: 125,
                   // url: '../../dataDictionary/updateData.action',
                    frame:false,
                    bodyStyle:'padding:0px 0px 0',
                    labelAlign:"right",
                    html:"<iframe src='tddData.jsp?ddId=" + par_ddId +"&modId="+record.id+"' style='height:100%;width:100%'></iframe>"
                   // items: [code,name,describe,ddId,dddId,zmark]
                });
            }
            if (modwin != null) {
                modwin.destroy(true);
                modwin = null;
            }
            if (!modwin) {
                modwin = new Ext.Window({
                    id : 'modformWin',
                    title: '修改记录',
                    width: 530,
                    height:320,
                    minWidth: 200,
                    minHeight: 200,
                    xtype:'window',
                    maximizable : false,
                    closeAction : 'hide',
                    layout: 'fit',
                    plain:false,
                    closable:true,
                    modal:false,
                    border:false,
                    bodyStyle:'padding:5px;',
                    buttonAlign:'center',
                    items: modformPanel,
                    buttons: [{
                        handler: function() {
                            eval(save_fun())

                            modwin.hide();
                            /*
                            if (modformPanel.getForm().isValid()) {
                                modformPanel.getForm().submit({
                                    waitMsg: '正在处理，请稍等...',
                                    reset: true,
                                    failure: function(form, action) {
                                        Ext.MessageBox.hide();
                                        Ext.Msg.show({
                                            title: '错误',
                                            msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + action.result.msg + '</span>',
                                            buttons: Ext.MessageBox.OK,
                                            icon: Ext.MessageBox.WARNING
                                        });
                                    },
                                    success: function(form, action) {
                                        Ext.MessageBox.hide();
                                        Ext.Msg.show({
                                            title: '成功',
                                            msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + action.result.msg + '</span>',
                                            buttons: Ext.MessageBox.OK,
                                            icon: Ext.MessageBox.INFO
                                        });

                                        modwin.hide();
                                        gridStore.reload();
                                    },
                                    method:'POST'
                                });
                            }  */
                        },
                        text: '保存'
                    },{
                        handler: function() {
                            modwin.hide();
                        },
                        text: '取消'
                    }]
                });
            }
            modwin.show();

       // onAddClick('mod')
    }


     function onAddClick2(item){

         if (modformPanel != null) {
             modformPanel.destroy(true);
             modformPanel = null;
         }

         if (!modformPanel) {
             modformPanel = new Ext.FormPanel({
                 labelWidth: 125,
                 // url: '../../dataDictionary/updateData.action',
                 frame:false,
                 bodyStyle:'padding:0px 0px 0',
                 labelAlign:"right",
                 html:"<iframe src='tddData.jsp?ddId=" + par_ddId +"&modId="+"' style='height:100%;width:100%'></iframe>"
                 // items: [code,name,describe,ddId,dddId,zmark]
             });
         }
         if (modwin != null) {
             modwin.destroy(true);
             modwin = null;
         }
         if (!modwin) {
             modwin = new Ext.Window({
                 id : 'modformWin',
                 title: '增加记录',
                 width: 530,
                 height:320,
                 minWidth: 200,
                 minHeight: 200,
                 xtype:'window',
                 maximizable : false,
                 closeAction : 'hide',
                 layout: 'fit',
                 plain:false,
                 closable:true,
                 modal:false,
                 border:false,
                 bodyStyle:'padding:5px;',
                 buttonAlign:'center',
                 items: modformPanel,
                 buttons: [{
                     handler: function() {
                         eval(save_fun())
                         modwin.hide();

                     },
                     text: '保存'
                 },{
                     handler: function() {
                         modwin.hide();
                     },
                     text: '取消'
                 }]
             });
         }
         modwin.show();
     }



    function onDelClick(item) {
        var selectedCount = sm.getCount();
        if (selectedCount == 0) {
            Ext.Msg.alert('删除操作', '未选中要删除的数据，请选择要删除的数据！');
            return;
        } else {
            Ext.Msg.confirm('删除记录', '请确认要删除记录吗?按是将删除选中记录！', function(btn) {
                if (btn == 'yes') {
                    var records = sm.getSelections();
                    var _ids = '';
                    for (var i = 0; i < records.length; i++) {
                        _ids += records[i].id;
                        _ids += '_';
                    }
                    Ext.MessageBox.wait('正在处理，请稍等...');
                    Ext.Ajax.request({
                        url:  '../../dataDictionary/deleteTddData.action',
                        failure: function(result, request) {
                            Ext.MessageBox.hide();
                            Ext.Msg.show({
                                title: '错误',
                                msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '删除失败！' + '</span>',
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.WARNING
                            });
                        },
                        success: function(result, request) {
                            Ext.MessageBox.hide();
                            Ext.Msg.show({
                                title: '成功',
                                msg: '<span style="text-align:center;width:250px;margin:5 0 5 0;">' + '删除成功！' + '</span>',
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.INFO
                            });
                            gridStore.reload();
                        },
                        params: { deleteID: _ids,ddId:par_ddId}
                    });
                }
            });
        }
    }

    var gridStore = new Ext.data.Store({
        proxy: new Ext.data.ScriptTagProxy({
            url: '../../dataDictionary/buildDataJSONString.action?ddId=' + par_ddId
        }),
        reader: new Ext.data.JsonReader({
            root: 'results',
            totalProperty: 'total',
            id: 'dddId'
        }, [
            {name: 'dddId', mapping: 'dddId'},
            {name: 'ddId', mapping: 'ddId'},

            {name: 'code', mapping: 'code'},
            {name: 'name', mapping: 'name'},
            {name: 'zmark', mapping: 'zmark'},
            {name: 'describe', mapping: 'describe'}
        ]),
        remoteSort: false
    });
    var sm = new Ext.grid.CheckboxSelectionModel();
//    sm.locked = true;
   // var cm = new Ext.grid.LockingColumnModel([sm,{
    var cm = new Ext.ux.grid.LockingColumnModel([sm, {

        id: 'dddId',
        header: "主键",
        dataIndex: 'dddId',
        width: 60,
        sortable: true,
        hidden:true,
        css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
    },
        {
            id: 'code',
            header: "编码",
            dataIndex: 'code',
            width: 80,
            align:'center',
            sortable: false,
            locked:false,
            css: 'white-space:normal;border:#eee solid;border-width:0 1 0 1;'
        },
        {
            id: 'name',
            header: "名称",
            dataIndex: 'name',
            width: 250,
            align:'center',
            sortable: false,
            locked:false,
            renderer:function (value) {
                return "<div align=left>" + value + "</div>"
            },
            css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
        },{  id: 'describe',
        header: "说明",
        dataIndex: 'describe',
        align:'center',
        width: 370,
        hidden:false,
        sortable: false,
        locked:false,
        renderer:function (value) {
                return "<div align=left>" + value + "</div>"
            },
        css: 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
    }
          ,  {
            id: 'zmark',
            header: "标记",
            dataIndex: 'zmark',
            width: 60,
            align:'center',
            sortable: false,
            locked:false,
            css: 'white-space:normal;border:#eee solid;border-width:0 1 0 1;'
        }

    ]);

  //  var grid = new Ext.grid.LockingGridPanel({
     grid = new Ext.ux.grid.SSNJLOCKPAGEGrid({
        //:'grid-div',
        id:'grid',
        layout:'fit',
        store:gridStore ,
        loadMask: {msg:'正在加载数据，请稍候……'},
        cm:cm,
        sm: sm,
        height:gridHeight,
        viewConfig: {
            enableRowBody:false
            //            forceFit:true
        },
        bodyStyle:'width:100%;',
        tbar:[addButton, '-', modButton, '-', delButton],
        frame:false,
        border:false,
        stripeRows:true
        //bbar:pagingToolbar
    });

   // grid.render();
    grid.render(Ext.get('grid-div'));

    gridStore.load({params:{start:0, limit:pageSize}});})
